//
//  PotentialDetail.swift
//  HuaYun
//
//  Created by enghou on 2018/10/11.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class PotentialDetailViewController: PagedViewController {
    
    var id: String = ""
    
    let button = UIButton.init(type: .system)
    
    override func configTitles()->Array<String> {
        return ["基本信息","最新动态"]
    }
    override func configViewControllers() -> [UIViewController] {
        
        let baseinfo = TitleContentTableViewController()
        
        let contents = [["潜在项目编号":""],["潜在项目名称":""],["客户单位":""],["客户单位负责人":""],["负责人联系方式":""],["业务类别/产品类型/项目大类":""],["项目类型":""],["项目预算(元)":""],["客户经理":""],["客户经理电话":""],["开始时间":""],["计划结束日期":""],["潜在项目状态":""],["归属事业部":""],["售前项目经理":""],["事业部协作情况":""],["项目目标":""],["项目简介":""],["备注":""]]
        
        baseinfo.source = contents
        
        let predicateinfo = NewestDynamicViewController()
        
        return [baseinfo,predicateinfo]
        
    }
    override func path(for name: String) -> String {
        return PotentialProjectDetail
    }
    override func param(for name: String) -> Dictionary<String, Any> {
        return ["id":self.id]
    }
    override func success(for name: String) -> NetSuccess {
        func suc(json: JSON)->Void{
            if let success = json.contents?["success"]?.bool {
                
                if success == true {
                    
                    let potential = self.ctls[0] as! TitleContentTableViewController
                    
                    if let _ = json.contents?["data"]?.contents?["biddinginfo"] {
                        var contents: Array<Dictionary<String,String?>> = []
                        contents.append(["潜在项目编号": json.contents?["data"]?.contents?["serialNumber"]?.rawString()])
                        contents.append(["潜在项目名称": json.contents?["data"]?.contents?["name"]?.rawString()])
                        contents.append(["客户单位": json.contents?["data"]?.contents?["customerUnit"]?.rawString()])
                        contents.append(["客户单位负责人": json.contents?["data"]?.contents?["customerUnitHead"]?.rawString()])
                        contents.append(["负责人联系方式": json.contents?["data"]?.contents?["headPhone"]?.rawString()])
                        contents.append(["业务类别/产品类型/项目大类": json.contents?["data"]?.contents?["headPost"]?.rawString()])
                        contents.append(["项目类型": json.contents?["data"]?.contents?["projectTypeName"]?.rawString()])
                        let projectBudget = json.contents?["data"]?.contents?["projectBudget"]?.int ?? 0
                        let projectBudgetStr = String(describing: projectBudget / 100)
                        contents.append(["项目预算(元)": projectBudgetStr.rmbformat])
                        contents.append(["客户经理": json.contents?["data"]?.contents?["customerManagerName"]?.rawString()])
                        contents.append(["客户经理电话": json.contents?["data"]?.contents?["phone"]?.rawString()])
                        contents.append(["开始时间": json.contents?["data"]?.contents?["startDate"]?.rawString()])
                        contents.append(["计划结束日期": json.contents?["data"]?.contents?["planEndDate"]?.rawString()])
                        let state = json.contents?["data"]?.contents?["state"]?.int ?? 0
                        var statestr = ""
                        switch state {
                        case 0:
                            statestr = "前期准备"
                            break
                        case 1:
                            statestr = "已立项"
                            break
                        case 2:
                            statestr = "已启动"
                            break
                        case 3:
                            statestr = "已结束"
                            break
                        case 4:
                            statestr = "已验收"
                            break
                        case 5:
                            statestr = "已结项"
                            break
                        case 6:
                            statestr = "终止或挂起"
                            break
                        default:
                            break
                            
                        }
                        contents.append(["潜在项目状态": statestr])
                        contents.append(["归属事业部": json.contents?["data"]?.contents?["belongDeptOrgName"]?.rawString()])
                        contents.append(["售前项目经理": json.contents?["data"]?.contents?["preSaleProjManagerName"]?.rawString()])
                        contents.append(["事业部协作状况": json.contents?["data"]?.contents?["collaboration"]?.rawString()])
//                        contents.append(["项目目标": json.contents?["data"]?.contents?["projectObjectives"]?.rawString()])
                        contents.append(["项目简介": json.contents?["data"]?.contents?["introduction"]?.rawString()])
                        contents.append(["备注": json.contents?["data"]?.contents?["remarks"]?.rawString()])
                        potential.source = contents
                        potential.tableView.reloadData()
                    }
                    if let newests = json.contents?["data"]?.contents?["newestDynamics"]?.array {
                        let sectioned = self.ctls[1] as! NewestDynamicViewController
                        sectioned.source = newests
                        sectioned.tableView.reloadData()
                    }
                } else {
                    self.alert(json.contents?["errorMsg"]?.rawString())
                }
            }
        }
        return suc
    }
    
    override func method(for name: String) -> NetMethod {
        return .get
    }
    
    @objc func add()->Void{
        let add = AddDynamicViewController()
        add.applyId = self.id
        self.present(vc: add)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        button.backgroundColor = UIColor.mainColor
        
        button.setTitle("新增动态", for: .normal)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        
        button.setTitleColor(UIColor.white, for: .normal)
        
        button.addTarget(self, action: #selector(add), for: .touchUpInside)
        
        self.view.addSubview(button)
        
        self.load(with: "detail")
        
        NotificationCenter.default.addObserver(self, selector: #selector(refresh), name: NSNotification.Name("RefreshPotential"), object: nil)
        
    }
    
    @objc func refresh()->Void{
        self.load(with: "detail")
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        button.frame = CGRect.init(origin: CGPoint(x: 0, y: self.view.bottom - 50), size: CGSize(width: UIScreen.main.bounds.size.width, height: 50))
        self.page.view.frame = CGRect(origin: CGPoint(x: 0, y: 55), size: CGSize(width: self.view.width, height: self.view.height - 50 - 55))
    }
}
